package da.asclass.login
{
    import da.asclass.events.cl.CreateEvent;

    import flash.events.Event;
    import flash.events.EventDispatcher;

    import mx.rpc.events.FaultEvent;
    import mx.rpc.events.ResultEvent;
    import mx.rpc.remoting.RemoteObject;

    public class RemoteCreate extends EventDispatcher
    {
        private var userName:String = null;
        private var password:String = null;
        private var createUser:RemoteObject = null;
        public static const REGIST_SUCCESS:String = "REGIST_SUCCESS";
        public static const REGIST_FAILED:String = " REGIST_FAILED";
        public static const REMOTE_CALL_FAILED:String = "REMOTE_CALL_FAILED";

        private var ce:CreateEvent = null;

        public function RemoteCreate(txtUserName:String = null , txtPassword:String = null):void
        {
            this.userName = txtUserName;
            this.password = txtPassword;
            createUser = new RemoteObject("CreateUser");
            createUser.addEventListener(ResultEvent.RESULT , createUserRE);
            createUser.addEventListener(FaultEvent.FAULT , createUserFE);
            ce = new CreateEvent();
        }

        public function create():void
        {
            createUser.create(this.userName , this.password);
        }

        public function createUserRE(event:ResultEvent):void
        {
            switch (event.result)
            {
                case 1: //create success
                {
                    ce.resultType = RemoteCreate.REGIST_SUCCESS;
                    break;
                }
                case 2: //user name exist
                {
                    ce.resultType = RemoteCreate.REGIST_FAILED;
                    break;
                }
                default:
                {
                    ce.resultType = RemoteCreate.REMOTE_CALL_FAILED;
                }
            }
            this.dispatchEvent(ce);
        }

        public function createUserFE(event:FaultEvent):void
        {
            ce.resultType = RemoteCreate.REMOTE_CALL_FAILED;
            this.dispatchEvent(ce);
        }
    }
}